const express = require('express')

const app =express()
//处理body请求体的中间件
app.use(express.urlencoded())
app.use(express.json())

app.get('/getLazy',(request,response)=>{//模拟响应延时
    setTimeout(()=>{
        const token = request.headers.token
        console.log(token)
        if(!token){//如果请求的客户端没有token，就响应没有权限，需要重新输入密码
        return response.send({
            code:10002,
            msg:'没有权限',
            data:'null'

        })
    }

    const {userId} =request.query
    if(userId!='001'){
        return response.send({
            code:10001,
            msg:'查询失败',
            data:'null'

        })
    }

    response.send({
        code:10000,
        msg:'查询成功',
        data:{
            name:'gyl',
            age:18,
            sex:'female'
        }
    })
    },3000)

})

app.get('/user',(request,response)=>{
    const token = request.headers.token
    console.log(token)
    if(!token){//如果请求的客户端没有token，就响应没有权限，需要重新输入密码
        return response.send({
            code:10002,
            msg:'没有权限',
            data:'null'

        })
    }

    const {userId} =request.query
    if(userId!='001'){
        return response.send({
            code:10001,
            msg:'查询失败',
            data:'null'

        })
    }

    response.send({
        code:10000,
        msg:'查询成功',
        data:{
            name:'gyl',
            age:18,
            sex:'female'
        }
    })


})
app.post('/userpost',(request,response)=>{
    const {name,age,sex} =request.body
    if(!name||!age||!sex){
        return response.send({
            code:10001,
            msg:'添加失败',
            data:'null'

        })
    }

    response.send({
        code:10000,
        msg:'添加成功',
        data:'null'
    })
})
app.put('/userUpdate',(request,response)=>{
    // const {name,age,sex} =request.body 
   
    response.send({
        code:10000,
        msg:'更新成功',
        data:'null'
    })

})
app.delete('/userDelete',(request,response)=>{
    // const {userId} =request.query
    
    response.send({
        code: 10000,
        mes: "删除成功",
        data: 'null'
      })
    
})
app.get('/1',(request,response)=>{
    response.sendFile(__dirname+'/axios的使用第二遍.html')
})
app.get('/2',(request,response)=>{
    response.sendFile(__dirname+'/axios请求别名.html')
})
app.get('/3',(request,response)=>{
    response.sendFile(__dirname+'/0622-1.创建axios实例.html')
})
app.get('/4',(request,response)=>{
    response.sendFile(__dirname+'/axios失败返回值.html')
})
app.get('/5',(request,response)=>{
    response.sendFile(__dirname+'/0622-2.请求拦截器.html')
})
app.get('/6',(request,response)=>{
    response.sendFile(__dirname+'/0622-2.响应拦截器.html')
})
app.get('/7',(request,response)=>{
    response.sendFile(__dirname+'/0622-3.token练习.html')
})
app.get('/8',(request,response)=>{
    response.sendFile(__dirname+'/0622-4.进度条练习.html')
})
app.get('/9',(request,response)=>{
    response.sendFile(__dirname+'/0622-5.完整拦截器.html')
})
app.get('/10',(request,response)=>{
    response.sendFile(__dirname+'/0622-6.axios取消请求.html')
})


app.listen(8886,()=>{
    console.log('成功,http://127.0.0.1:8886')
})